import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
const originalPush = VueRouter.prototype.push
  VueRouter.prototype.push = function push(location) {
        return originalPush.call(this, location).catch(err => err)
      }
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/welcome',
    name: 'welcome',
    component: () => import( '../views/Welcome.vue')
  },{
    path: '/movie',
    name: 'Movie',
    component: () => import( '../views/Movie/Movie.vue'),
  },
  {
    path:"/movieMore/:keyword",
    name:'movieMore',
    component:()=>import("../views/Movie/MovieMore.vue")
  },
  {
    path:"/movieDetail/:id",
    name:"movieDetail",
    component:()=>import("../views/Movie/MovieDetail.vue")
  },
  {
    path: '/article',
    name: 'Article',
    component: () => import( '../views/Article/Article.vue'),
    
  },
  {
    path:"/articleDetail/:id",
    name:"articleDetail",
    component:()=>import("../views/Article/ArticleDetail.vue")
  },
  {
    path: '/center',
    name: 'Center',
    component: ()=>import("../views/Center/Center.vue"),
    children:[
      {
        path:"historylists",
        component:()=>import('../views/Center/Historylists.vue')
      },
      {
        path:"collectlists",
        component:()=>import('../views/Center/Collectlists.vue')
      },
    ]
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
  {
    path: '/*',
    component: () => import( '../views/Error.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

var vm = new Vue();
router.beforeEach((to,from,next)=>{
  console.log(document.cookie)  //loginAuth :true,也可以在f12控制台里敲document.cookie输出
  // console.log(vm.$cookie.get("loginAuth"))
  var auth = vm.$cookie.get("loginAuth");
  // console.log(to.path)//可以拿到路由路径
  if(to.path == "/login"){
    if(auth){
      router.push("home")
    }else{
      next()
    }
  }else{
    if(auth){
      //不在登录页
      next();
    }else{
      router.push("/login")
    }
  }
  next()
})

export default router
